home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 101_01 / polish.c < prev    next >
Text File  |  1985-11-13  |  2KB  |  108 lines

  1. /*  This is the RPN calculator taken from the C Manual
  2. by Kernighan & Ritchie.  It's an excellent example of stack
  3. implementation. It works for signed numbers. For unsigned
  4. numbers, just change the d in the "=" case print statement
  5. to a u. Example of use:
  6. 1 2 - 4 5 + * =  -9
  7. */
  8.  
  9. #define MAXOP 20
  10. #define NUMBER '0'
  11. #define TOOBIG '9'
  12. int stkpntr;
  13.  
  14. main ()
  15. {
  16.     stkpntr = 0;
  17.     int type;
  18.     char s[MAXOP];
  19.     int op2, atoi(), pop(), push();
  20.  
  21.     while (type = getop(s,MAXOP))
  22.         switch (type)  {
  23.         case NUMBER:
  24.             push(atoi(s));
  25.             break;
  26.         case '+':
  27.             push(pop() + pop());
  28.             break;
  29.         case '*':
  30.             push(pop() * pop());
  31.             break;
  32.         case '-':
  33.             op2 = pop();
  34.             push(pop() - op2);
  35.             break;
  36.         case '/':
  37.             op2 = pop();
  38.             if (op2 != 0)
  39.                 push(pop() / op2);
  40.             else
  41.                 printf("Zero divisor popped\n");
  42.             break;
  43.         case '=':
  44.             printf("\t%d\n", push(pop()));
  45.             break;
  46.         case 'c':
  47.             clear();
  48.             break;
  49.         case TOOBIG:
  50.             printf("%.20s ... is too long\n", s);
  51.             break;
  52.         default:
  53.             printf("  Unknown command %c\n", type);
  54.             break;
  55.         }
  56. }
  57. #define MAXVAL 100
  58. int val[MAXVAL];
  59. int push (f)
  60. int f;
  61. {
  62.     if (stkpntr < MAXVAL)
  63.         return(val[stkpntr++] = f);
  64.     else  {
  65.         printf("Error: stack full\n");
  66.         clear ();
  67.         return (0);
  68.     }
  69. }
  70. int pop ()
  71. {
  72.     if (stkpntr > 0)
  73.         return (val[--stkpntr]);
  74.     else  {
  75.         printf("Error: stack empty\n");
  76.         clear ();
  77.         return(0);
  78.     }
  79. }
  80. clear ()
  81. {
  82.     stkpntr = 0;
  83. }
  84. getop(s,lim)
  85. char s[];
  86. int lim;
  87. {
  88.     int i,c;
  89.     while ((c = getchar()) == ' ' || c == '\t' || c == '\n')
  90.         ;
  91.     if  (c < '0' || c > '9')
  92.         return(c);
  93.     s[0] = c;
  94.     for (i = 1; (c = getchar()) >= '0' && c <= '9' ; i++)
  95.         if (i < lim)
  96.             s[i] = c;
  97.     if (i < lim) {
  98.         ungetch(c);
  99.         s[i] = '\0';
  100.         return(NUMBER);
  101.     } else { 
  102.         while (c != '\n')
  103.             c = getchar();
  104.         s[lim-1] = '\0';
  105.         return (TOOBIG);
  106.     }
  107. }
  108.